﻿#include "grid.h"
#include "screen.h"
#include <QDateTime>

//chua chay, cast qua cast lai
PlayGrid::PlayGrid(PlayScreen* parent) :
    Grid((Screen*) parent)
{
    p = parent;
    isPlaying = false;
    connect(p->buttonDone, SIGNAL(clicked(bool)), this, SLOT(Done(bool)));
    connect(p->buttonSave, SIGNAL(clicked()), this, SLOT(SaveGrid()));
}

int PlayGrid::Done(bool closing)
{
    QDialog* confrim = new QDialog();
    confrim->setGeometry(300, 200, 300, 100);
    confrim->setWindowTitle(QString::fromUtf8("Bạn có chắc chắn?"));
    confrim->setAttribute(Qt::WA_DeleteOnClose);
    QHBoxLayout* layout = new QHBoxLayout(confrim);
    QMapIterator<int, Cell*> i(cells);
    while (i.hasNext())
    {
        i.next();
        if(i.value()->cap->toPlainText().length() < 1)
        {
            confrim->setWindowTitle(QString::fromUtf8("Ô chử chưa hoàn thành, bạn chắc chứ?"));
            QPushButton* save = new QPushButton(QString::fromUtf8("Lưu lại"), confrim);
            save->setCheckable(true);
            save->setChecked(false);
            connect(save, SIGNAL(clicked(bool)), this, SLOT(SaveGrid(bool)));
            layout->addWidget(save);
            break;
        }
    }
    QPushButton* submit = new QPushButton(QString::fromUtf8("Xem kết quả"), confrim);
    connect(submit, SIGNAL(clicked()), this, SLOT(Result()));
    layout->addWidget(submit);

    QPushButton* cancel = new QPushButton(QString::fromUtf8("Hủy"), confrim);
    cancel->setDefault(true);
    connect(cancel, SIGNAL(clicked()), confrim, SLOT(reject()));
    layout->addWidget(cancel);

    if(closing)
    {
        QPushButton* quit = new QPushButton(QString::fromUtf8("Thoát"), confrim);
        cancel->setDefault(false);
        quit->setDefault(true);
        connect(quit, SIGNAL(clicked()), this, SLOT(Quit()));
        layout->addWidget(quit);
    }
    confrim->setLayout(layout);
    return confrim->exec();
}

void PlayGrid::SaveGrid(bool type)
{
    Save(2, p->descLabel->text().append(QString::fromUtf8("|| đang chơi ngày ")).append(
             QDateTime::currentDateTime().toString()));
    if(type)
    {
        QDialog* pd = (QDialog*) this->sender()->parent();
        pd->accept();
    }
}

void PlayGrid::Result()
{
    int total = cells.size();
    int correct = total;
    QMapIterator<int, Cell*> i(cells);
    while (i.hasNext())
    {
        i.next();
        if(i.value()->cap->toPlainText() != i.value()->ans->toPlainText())
        {
            correct--;
            i.value()->ans->show();
        }
    }
    QLabel* report = new QLabel(QString::fromUtf8("Đúng %1 trên tổng số %2 ký tự.\nTổng cộng %3 điểm.").arg(correct).arg(total).arg(correct*100/total),
                                p);
    report->setStyleSheet("color: blue; background-color: red");
    p->layoutLeft->addWidget(report);
    QDialog* pd = (QDialog*) this->sender()->parent();
    pd->reject();
    isPlaying = false;
}
void PlayGrid::Quit()
{
    QDialog* pd = (QDialog*) this->sender()->parent();
    pd->accept();
}
